﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using Word = Microsoft.Office.Interop.Word;

namespace TextMIS
{
    public partial class frmPrint : Form
    {
        public frmPrint()
        {
            InitializeComponent();
        }

        private StringReader myReader;
        public RichTextBox richTextBox1;
        private frmPrint2 f; 

        private void frmPrint_Load(object sender, EventArgs e)
        {
            this.myStatu.Text = "";

            f = new frmPrint2();

            f.Owner = this;

            f.Hide(); 
        }


        private void CheckSave()
        {

            if (this.richTextBox1.Text != "")
            {

                if (MessageBox.Show("是否保存当前文件?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {

                    SaveFileDialog svfDialog = new SaveFileDialog();

                    svfDialog.Filter = "文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";

                    if (svfDialog.ShowDialog() == DialogResult.OK)
                    {
                        this.richTextBox1.SaveFile(svfDialog.FileName, RichTextBoxStreamType.PlainText);

                    }

                }

            }

        }

        private void mnNew_Click(object sender, EventArgs e)
        {
            this.myStatu.Text = "新建文件"; 

            this.CheckSave();

            this.richTextBox1.Clear();

        }

        private void mnOpen_Click(object sender, EventArgs e)
        {
            this.myStatu.Text = "打开文件"; 

            this.CheckSave();

            richTextBox1.Clear();

            OpenFileDialog opfDialog = new OpenFileDialog();

            opfDialog.Filter = "文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";
//            opfDialog.Filter = "所有文件|*.*";

            if (opfDialog.ShowDialog() == DialogResult.OK)
            {
                //this.richTextBox1.LoadFile(opfDialog.FileName, RichTextBoxStreamType.PlainText);
                OpenWord(opfDialog.FileName);

            }

        }

        private void mnSave_Click(object sender, EventArgs e)
        {
            this.myStatu.Text = "保存文件";

            SaveFileDialog svfDialog = new SaveFileDialog();

//            svfDialog.Filter = "文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";

            svfDialog.Filter = "所有文件|*.doc*";

            if (svfDialog.ShowDialog() == DialogResult.OK)
            {

                this.richTextBox1.SaveFile(svfDialog.FileName, RichTextBoxStreamType.PlainText);

            } 
        }

        private void MyPrintDC_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //打印文档打印页面事件代码 

            this.myReader = new StringReader(this.richTextBox1.Text);//定义字符读流 

            Graphics myGraphics = e.Graphics;

            Font myPrintFont = this.richTextBox1.Font;

            //计算一页行数 

            float iLinePage = e.MarginBounds.Height / myPrintFont.GetHeight(myGraphics);

            int iLineNumber = 0;//打印行数 

            float fyPosition = 0;//打印时的纵坐标 

            float fMarginLeft = e.MarginBounds.Left;//纸页面左边界 

            float fMarginTop = e.MarginBounds.Top;

            string strLine = "";

            while ((iLineNumber < iLinePage) && (strLine = myReader.ReadLine()) != null)
            {

                fyPosition = fMarginTop + iLineNumber * myPrintFont.GetHeight(myGraphics);

                myGraphics.DrawString(strLine, myPrintFont, new SolidBrush(Color.Black), fMarginLeft, fyPosition, new StringFormat());

                iLineNumber++;

            }
            if (strLine != null)
            {

                e.HasMorePages = true;

            }

            else
            {

                e.HasMorePages = false;

            } 
        }

        private void mnPageSetup_Click(object sender, EventArgs e)
        {
            this.myStatu.Text = "打印设置";
            PageSetupDialog mypgDialog = new PageSetupDialog();

            mypgDialog.Document = this.MyPrintDC;

            try
            {

                mypgDialog.ShowDialog();

            }

            catch
            {

                this.MyPrintDC.PrintController.OnEndPrint(this.MyPrintDC, new System.Drawing.Printing.PrintEventArgs());

            } 
        }

        private void mnPrintView_Click(object sender, EventArgs e)
        {
            this.myStatu.Text = "打印预览";

            PrintPreviewDialog myptViewDialog = new PrintPreviewDialog();

            myptViewDialog.Document = this.MyPrintDC;

            try
            {

                myptViewDialog.ShowDialog();

            }

            catch
            {

                this.MyPrintDC.PrintController.OnEndPrint(this.MyPrintDC, new System.Drawing.Printing.PrintEventArgs());

            } 
        }

        private void mnPint_Click(object sender, EventArgs e)
        {
            this.myStatu.Text = "打印...";
            PrintDialog ptDialog = new PrintDialog();

            ptDialog.Document = this.MyPrintDC;

            if (ptDialog.ShowDialog() == DialogResult.OK)
            {

                try
                {

                    this.MyPrintDC.Print();

                }

                catch
                {

                    this.MyPrintDC.PrintController.OnEndPrint(this.MyPrintDC, new System.Drawing.Printing.PrintEventArgs());

                }

            } 
        }

        private void mnCopy_Click(object sender, EventArgs e)
        {
            if (this.richTextBox1.SelectedText != "")
            {

                Clipboard.SetDataObject(this.richTextBox1.SelectedText);

                this.mnCopy.Enabled = false;

                this.mnCut.Enabled = false;

                this.mnPaste.Enabled = true;

            } 

        }

        private void mnCut_Click(object sender, EventArgs e)
        {
            if (this.richTextBox1.SelectedText != "")
            {

                Clipboard.SetDataObject(this.richTextBox1.SelectedText);

                this.richTextBox1.SelectedText = "";

                this.mnCopy.Enabled = false;

                this.mnCut.Enabled = false;

                this.mnPaste.Enabled = true;

            } 
        }

        private void mnPaste_Click(object sender, EventArgs e)
        {
            IDataObject d = Clipboard.GetDataObject();

            this.richTextBox1.SelectedText = (string)d.GetData(DataFormats.Text); 
        }

        private void mnSearch_Click(object sender, EventArgs e)
        {
            f.Show(); 
        }

        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            if (this.richTextBox1.SelectedText != "")
            {

                this.mnCut.Enabled = true;

                this.mnCopy.Enabled = true;

            }

            else
            {

                this.mnCut.Enabled = false;

                this.mnCopy.Enabled = false;

                this.mnPaste.Enabled = true;

            } 
        } 
        public void OpenWord(string fileName)  
        {  
            Word.ApplicationClass app = new Word.ApplicationClass();  
            Word._Document doc = null;  
            object missing = System.Reflection.Missing.Value;  
            object File = fileName;    
            object readOnly = false;  
            object isVisible = true;  
            try  
                        
            {  
                            
                doc = app.Documents.Open(ref File, ref missing, ref readOnly,  
                           
                    ref missing, ref missing, ref missing, ref missing, ref missing,  
                             
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,  
                             
                    ref missing, ref missing, ref missing);  
                      
                            
                doc.ActiveWindow.Selection.WholeStory();//全选word文档中的数据  
                            
                doc.ActiveWindow.Selection.Copy();//复制数据到剪切板  
                            
                richTextBox1.Paste();//richTextBox粘贴数据  
                            //richTextBox1.Text = doc.Content.Text;//显示无格式数据  
                        
            }  
                       
            finally  
                        
            {  
                            
                if (doc != null)  
                           
                {  
                             
                    doc.Close(ref missing, ref missing, ref missing); 
                               
                               
                    doc = null;  
                    
                }  
                
                
                if (app != null)  
                
                {  
                
                    app.Quit(ref missing, ref missing, ref missing);  
                    
                    app = null;  
                    
                }  
                
            }  
            
        }

                    private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
                    {
                        this.Close();
                    } 

 
    }
}
